package com.srq.controller;

import com.srq.pojo.Goods;
import com.srq.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;
    @RequestMapping("/list")
    public ModelAndView findAllGoods(){
        ModelAndView modelAndView= new ModelAndView();
        List<Goods> goodsList = goodsService.findAllGoods();
        modelAndView.addObject("goodsList",goodsList);
        modelAndView.setViewName("user-list");
        return modelAndView;
    }
    @RequestMapping("/save")
    public ModelAndView addGoods(Goods goods){
        ModelAndView modelAndView= new ModelAndView();
        boolean goods1 = goodsService.findGoods(goods);
        if (goods1){
            goodsService.addGoods(goods);
            modelAndView.setViewName("redirect:/goods/list");

        }
        else {
            modelAndView.setViewName("user-add");
            modelAndView.addObject("b",false);
        }
       return modelAndView;

    }
    @RequestMapping("/findName")
    public ModelAndView findGoodsByName(String name){
        ModelAndView modelAndView = new ModelAndView();
        List<Goods> goodsList=goodsService.findGoodsByName(name);
        modelAndView.addObject("goodsList",goodsList);
        modelAndView.setViewName("user-list");
        return modelAndView;
    }
    @RequestMapping("/del")
    public String deleteGoods(Integer id){
        goodsService.deleteGoods(id);
        return "redirect:/goods/list";

    }
}
